package main

import (
	"core/env"
	"core/initial"
	"core/utils"
	"os"

	"strings"

	"github.com/fvbock/endless"
	"github.com/gin-gonic/gin"
)

var (
	runMode string
)

func init() {
	args := os.Args
	if len(args) < 2 {
		runMode = env.RunModeDev
	} else {
		switch args[1] {
		case "testCore":
			runMode = env.RunModeTestCore
		case "testDB":
			runMode = env.RunModeTestDB
		case "test":
			runMode = env.RunModeTest
		case "local":
			runMode = env.RunModeLocal
		case "dev":
			runMode = env.RunModeDev
		case "prod":
			runMode = env.RunModeProd
		case "adminProdJigsaw":
			runMode = env.RunModeAdminProdJigsaw
		default:
			utils.Error("请输入正确的环境变量")
			os.Exit(1)
		}
	}

}

func main() {
	closeFunc, err := initial.Init(runMode)
	utils.Check("init.InitConf", err)
	defer closeFunc()

	if strings.Contains(string(runMode), "prod") {
		gin.SetMode(gin.ReleaseMode)
	}

	// 初始化路由
	router := InitRouter()

	addr := "localhost:" + env.Conf.ServerPORT
	utils.Info("start:", addr, runMode)
	err = endless.ListenAndServe(addr, router)
	utils.Check("ListenAndServe", err)
}
